package com.atguigu.rest.controller;

import com.atguigu.rest.bean.Department;
import com.atguigu.rest.bean.Employee;
import com.atguigu.rest.dao.DepartmentDao;
import com.atguigu.rest.dao.EmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Date:2020/9/29
 * Author:ybc
 * Description:
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 查询所有的员工信息，即列表功能
     * @return
     */
    @RequestMapping(value = "/employees", method = RequestMethod.GET)
    public String getEmployeeList(Map<String, Object> map){
        //查询所有员工信息
        Collection<Employee> employeeList = employeeDao.getAll();
        //将所有的员工信息放在请求域中共享
        map.put("employeeList", employeeList);
        //设置视图名称，实现页面跳转
        return "list";
    }

    /**
     * 跳转到添加页面，同时获取所有的部门信息，在页面中以下拉框的方式展示，供用户选择
     * @param map
     * @return
     */
    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd(Map<String, Object> map){
        //获取所有的部门信息
        Collection<Department> departmentList = departmentDao.getDepartments();
        //创建存储了性别信息的map
        Map<String, String> genders = new HashMap<>();
        genders.put("1", "male");
        genders.put("0", "female");
        //将部门信息和性别信息在请求域中进行共享
        map.put("departmentList", departmentList);
        map.put("genders", genders);
        //form标签固定的规则，指定用于回显的请求域数据，且属性名必须为command；可以通过form:form标签中的commandName属性，设置用户回显的数据的属性名
        //map.put("command", new Employee());
        map.put("employee", new Employee());
        //设置视图名称，实现页面跳转
        return "add";
    }

    /**
     * 编辑员工信息，成功跳转到列表功能
     * @return
     */
    @RequestMapping(value = "/employee", method = {RequestMethod.POST, RequestMethod.PUT})
    public String editEmployee(Employee employee){
        //实现添加或修改功能
        employeeDao.save(employee);
        //跳转到列表功能
        return "redirect:/employees";
    }

    /**
     * 删除员工信息，成功跳转到列表功能
     * @param id
     * @return
     */
    @RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id){
        //删除员工信息
        employeeDao.delete(id);
        //跳转到列表功能
        return "redirect:/employees";
    }

    /**
     * 跳转到修改页面，并获取要修改的员工信息和所有的部门信息进行回显
     * @return
     */
    @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable("id") Integer id, Map<String, Object> map){
        //获取要修改的员工信息
        Employee employee = employeeDao.get(id);
        //获取所有的部门信息
        Collection<Department> departmentList = departmentDao.getDepartments();
        //创建存储了性别信息的map
        Map<String, String> genders = new HashMap<>();
        genders.put("1", "male");
        genders.put("0", "female");
        //在请求域中共享要修改的员工信息和所有的部门信息以及存储了性别信息的map
        map.put("employee", employee);
        map.put("departmentList", departmentList);
        map.put("genders", genders);
        //跳转到修改页面
        return "update";
    }

    /*@RequestMapping(value = "/employee", method = RequestMethod.PUT)
    public String updateEmployee(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employees";
    }*/

}
